package com.future.hedongjun.mine.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.utils.GlideUtils;
import com.future.baselib.utils.ToastUtils;
import com.future.baselib.view.IosAlertDialog;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.mine.bean.FileBean;
import com.future.hedongjun.mine.bean.FileType;
import com.future.hedongjun.mine.bean.MyVideo;
import com.future.hedongjun.mine.utils.FileUtil;
import com.future.hedongjun.mine.utils.MyVideoThumbLoader;
import com.kingsoft.media.httpcache.KSYProxyService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liubohua on 16/8/24.
 */
public class CachedAdapter extends BaseAdapter {
    private List<FileBean> mycachedlist = new ArrayList<FileBean>();
    private LayoutInflater mLayoutInflater;
    private MyVideoThumbLoader mVideoThumbLoader;
    private Context mContent;
    private KSYProxyService proxyService;

    public CachedAdapter(Context context, KSYProxyService proxy) {
        this.mContent = context;
        this.proxyService = proxy;
        mLayoutInflater = LayoutInflater.from(context);
        mVideoThumbLoader = new MyVideoThumbLoader();
    }

    @Override
    public int getCount() {
        return mycachedlist.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            view = mLayoutInflater.inflate(R.layout.recycler_item_cache, null);
            holder.img = (ImageView) view.findViewById(R.id.tv_cacheIcon);
            holder.title = (TextView) view.findViewById(R.id.tv_cacheName);
            holder.url = (TextView) view.findViewById(R.id.tv_cacheNum);
            holder.delete = view.findViewById(R.id.tv_delete);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        String path = mycachedlist.get(i).getPath();
        if (path != null) {
            holder.img.setTag(path);
            mVideoThumbLoader.showThumbByAsynctack(path, holder.img);
        } else {
            GlideUtils.loadImage(mContent, mycachedlist.get(i).getClassLogo(), holder.img);
        }

        holder.title.setText(mycachedlist.get(i).getName());
        if (mycachedlist.get(i).getFileType() == FileType.directory) {
            holder.url.setText(mycachedlist.get(i).getChildCount() + "个视频");
            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new IosAlertDialog(mContent)
                            .builder()
                            .setMsg("清除缓存后，需要重新下载哦\n" +
                                    "确定清除么？")
                            .setNegativeButton("取消", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {

                                }
                            })
                            .setPositiveButton("确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    FileUtil.delete(mycachedlist.get(i).getPath(), mContent);
                                    for (int i = 0; i < mycachedlist.size(); i++) {
                                        proxyService.cleanCache(mycachedlist.get(i).getPath());
                                    }
                                    mycachedlist.remove(i);
                                    notifyDataSetChanged();
                                }
                            }).show();
                }
            });
        } else {
            holder.url.setText("");
            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new IosAlertDialog(mContent)
                            .builder()
                            .setMsg("清除缓存后，需要重新下载哦\n" +
                                    "确定清除么？")
                            .setNegativeButton("取消", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                }
                            })
                            .setPositiveButton("确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    FileUtil.deleteSingleFile(mycachedlist.get(i).getPath(), mContent);
                                    proxyService.cleanCache(mycachedlist.get(i).getUrl());
                                    mycachedlist.remove(i);
                                    notifyDataSetChanged();
                                }
                            }).show();
                }
            });
        }
        return view;
    }

    public final class ViewHolder {
        public ImageView img;
        public TextView title;
        public TextView url;
        public TextView delete;
    }

    public void addData(List<FileBean> list) {
        this.mycachedlist.addAll(list);
        notifyDataSetChanged();
    }

    public void cleanData() {
        if (mycachedlist.size() > 0) {
            mycachedlist.clear();
            notifyDataSetChanged();
        }
    }

    public void refresh(List<FileBean> list) {
        this.mycachedlist = list;
        notifyDataSetChanged();
    }
}
